//
//  XHSettingViewController.m
//  xinhua
//
//  Created by fengyn on 15/10/29.
//  Copyright © 2015年 lexingsoft. All rights reserved.
//

#import "XHSettingViewController.h"
#import "XHFollowUsViewController.h"
#import "XHContactUsViewController.h"
#import "XHShareViewController.h"

#import "XHSettingCell.h"
#import "XHExitCell.h"
#import "APIKey.h"

#import <UIActionSheet+Blocks.h>
#import <UIAlertView+Blocks.h>
#import <SDImageCache.h>

#import "AppDelegate.h"

@interface XHSettingViewController ()<XHSettingCellDelegate,XHSettingCellDelegate>
@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;

@end

@implementation XHSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.ui_tableView.backgroundColor = [ColorManager colorWithHexString:@"F4F4F4"];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHSettingCell" bundle:nil] forCellReuseIdentifier:XHSettingCellIdentifier];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHExitCell" bundle:nil] forCellReuseIdentifier:XHExitCellIdentifier];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)clearDiskCache{
    RIButtonItem *okItem=[RIButtonItem itemWithLabel:@"确认" action:^{
        [[SDImageCache sharedImageCache] clearDisk];
        [self.ui_tableView reloadData];
    }];
    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
    UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"" message:@"确认清除缓存吗?" cancelButtonItem:cancelItem otherButtonItems:okItem, nil];
    [alertView show];
}


- (void)exitAction{
    [[DataManager sharedInstance] clearUserDefaults];
    [self.navigationController popToRootViewControllerAnimated:YES];
//    AppDelegate *delegate=[UIApplication sharedApplication].delegate;
//    [delegate goLoginAction];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([[DataManager sharedInstance] isLogin]) {
        return 2;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rows=0;
    switch (section) {
        case 0:
            rows=5;
            break;
        case 1:
            rows=1;
            break;
    }
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (indexPath.section==0) {
        cell=[self configOtherCellWithIndexPath:indexPath];
    }else{
        cell=[self configExitCell];
    }
    return cell;
}

-(XHExitCell *)configExitCell
{
    XHExitCell *cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHExitCellIdentifier];
//    DataManager *dm=[DataManager sharedInstance];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    [cell setBackgroundColor:[ColorManager colorWithCellBackground]];
    return cell;
}

-(XHSettingCell *)configOtherCellWithIndexPath:(NSIndexPath *)indexPath
{
    XHSettingCell *cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHSettingCellIdentifier];
    NSUInteger section=indexPath.section;
    NSUInteger row=indexPath.row;
    cell.ui_detailLabel.hidden=YES;
    cell.ui_switch.hidden=YES;
    switch (section) {
            break;
        case 0:
        {
            switch (row)
            {
                case 0:
                    cell.ui_titleLabel.text=@"清除本地缓存";
                    cell.ui_detailLabel.hidden=NO;
                    cell.ui_line.hidden = NO;
                    cell.ui_jianTou.hidden = YES;
                    cell.ui_detailLabel.text=[YNFileMethod convertSize:[SDImageCache sharedImageCache].getSize];
                    break;
                case 1:
                    cell.ui_titleLabel.text=@"给个好评";
                    cell.ui_line.hidden = NO;
                    cell.ui_jianTou.hidden = NO;
                    break;
                case 2:
                    cell.ui_titleLabel.text=@"联系我们";
                    cell.ui_line.hidden = NO;
                    cell.ui_jianTou.hidden = NO;
                    break;
                case 3:
                    cell.ui_titleLabel.text=@"关于我们";
                    cell.ui_line.hidden = NO;
                    cell.ui_jianTou.hidden = NO;
                    break;
                case 4:
                    cell.ui_titleLabel.text =@"分享APP";
                    cell.ui_line.hidden = YES;
                    cell.ui_jianTou.hidden = NO;
                    break;
            }
        }
            break;
    }
    cell.delegate=self;
    [cell setBackgroundColor:[ColorManager colorWithCellBackground]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//        if(![[DataManager sharedInstance] isLogin])
//        {
//            [self goLogin];
//            return;
//        }
    NSInteger section=indexPath.section;
    NSInteger row=indexPath.row;
    
    switch (section) {
        case 0:
        {
            switch (row) {
                case 0:
                    //缓存
                {
                    [self clearDiskCache];
                }
                    break;
                case 1:
                    //给个好评
                {
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesURL]];
                }
                    break;
                case 2:
                    //联系我们
                {
                    XHContactUsViewController * us = [[XHContactUsViewController alloc]init];
                    us.title =@"联系我们";
                    [self.navigationController pushViewController:us animated:YES];
                }
                    break;
                case 3:
                    //关于我们
                {
                    XHFollowUsViewController * us = [[XHFollowUsViewController alloc]init];
                    us.title = @"关于我们";
                    [self.navigationController pushViewController:us animated:YES];
                }
                    break;
                case 4:
                {
                    XHShareViewController * share = [[XHShareViewController alloc]init];
                    share.modalPresentationStyle = UIModalPresentationCustom;
                    share.shareType = @"app";
                    [self presentViewController:share animated:NO completion:^{
                        
                    }];

                }
                    break;
            }
        }
            break;
        case 1:
        {
            switch (row) {
                case 0://退出
                {
                    WS(weakSelf)
                    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
                    RIButtonItem *exitItem=[RIButtonItem itemWithLabel:@"退出" action:^{
                        [weakSelf exitAction];
                    }];
                    UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:@"确定要退出吗?" cancelButtonItem:cancelItem destructiveButtonItem:exitItem otherButtonItems:nil];
                    [actionSheet showInView:self.view];
                }
                    break;
            }
        }
            break;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=55;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    CGFloat height=0;
    if (section==0)
    {
        height=0.1;
    }
    else if(section==1)
    {
        height=5;
    }
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    CGFloat height=5;
    return height;
}

#pragma mark - XHExitCellDelegate

- (void)exitCellAction:(UIButton*)button
{
    WS(weakSelf)
    RIButtonItem *cancelItem=[RIButtonItem itemWithLabel:@"取消"];
    RIButtonItem *exitItem=[RIButtonItem itemWithLabel:@"退出" action:^{
        [weakSelf exitAction];
    }];
    UIActionSheet *actionSheet=[[UIActionSheet alloc] initWithTitle:@"确定要退出吗?" cancelButtonItem:cancelItem destructiveButtonItem:exitItem otherButtonItems:nil];
    [actionSheet showInView:self.view];
}

#pragma mark - XHSettingCellDelegate
- (void)switchAction:(id)sender {
    BOOL isOn=((UISwitch *)sender).isOn;
    [DataManager sharedInstance].isSubscribeActivityPushMessage=isOn;
    if (isOn) {
        /*
        [YunBaService subscribe:@"activityPush" resultBlock:^(BOOL succ, NSError *error) {
            
        }];
         */
    }else{
        /*
        [YunBaService unsubscribe:@"activityPush" resultBlock:^(BOOL succ, NSError *error) {
            
        }];
         */
    }
}
@end
